<template>
  <div class="container">
    <el-card class="box-card">
      <div class="text item">
        <div style="font-size: 17px">消息通知</div>
        <div style="margin-top: 10px">
          访客/会员动态即使触发，提高网站互动效率
        </div>
      </div>
    </el-card>
    <div>
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="名称" prop="channel_name" />
        <el-table-column label="通道说明" prop="channel_explain" />
        <el-table-column label="通知类型" prop="channel_type_name" />

        <el-table-column label="配置" prop="address">
          <template slot-scope="scoped">
            <el-button
              v-permissions="{ permission: ['add:NotifySetting'] }"
              type="primary"
              @click="details(scoped.row)"
            >
              设置
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog
        :before-close="handleClose"
        title=""
        :visible.sync="dialogVisible"
        width="30%"
      >
        <el-table
          ref="multipleTable"
          :data="userData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            :reserve-selection="true"
            type="selection"
            width="55"
          />

          <el-table-column label="姓名" prop="username" />
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="Submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { getUserList } from '@/api/public'
  import { msgChannel, distributeMsgChannel } from '@/api/channel'

  export default {
    name: 'VabErrorLog',
    data() {
      return {
        dialogVisible: false,
        userData: [],
        emptyText: ' ',
        tableData: [],
        user_ids: '',
        msg_channel_id: '',
        arr: [],
      }
    },
    computed: {},
    watch: {},
    beforeMount() {},
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        getUserList().then((e) => {
          this.userData = e.data
        })
        msgChannel().then((e) => {
          this.tableData = e.data
          loading.close()
          if (e.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },

      Submit() {
        const data = {
          msg_channel_id: this.msg_channel_id,
          user_ids: this.user_ids,
        }
        distributeMsgChannel(data).then(() => {
          this.dialogVisible = false
          this.init()
        })
      },
      details(e) {
        this.msg_channel_id = e.id
        const arr = []
        e.user_msg_channel.forEach((j) => {
          this.userData.forEach((i) => {
            if (j.user_id == i.id) {
              arr.push(i)
            }
          })
        })
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.multipleTable.clearSelection()
          this.toggleSelection(arr)
        })
      },

      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },

      toggleSelection(arr) {
        if (arr) {
          arr.forEach((row) => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange(val) {
        const arr = []
        val.forEach((j) => {
          arr.push(j.id)
        })
        this.user_ids = arr.toString()
        this.multipleSelection = val
      },
    },
  }
</script>

<style scoped>
  .container {
    padding: 15px;
  }
  table.tftable,
  table.table-count {
    font-size: 12px;
    color: #333333;
    width: 100%;
    border-width: 1px;
    border-collapse: collapse;
  }

  table.table-count th,
  table.table-count td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: center;
  }

  table.tftable th,
  table.tftable td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: left;
    text-align: center;
  }

  .tftable {
    margin-left: 1%;
    margin-bottom: 10px;
    margin-top: 10px;
  }
  table.tftable tr:hover {
    background: #c3ecff;
  }
  .tftable > thead > tr:hover {
    background: white;
  }
</style>
